<?php

/**
 * Modello Utenti - LaFabbrica.org
 *
 * Le funzioni principali, fetchAll() ecc. sono generate da Classes_DbTable_Table
 *
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 * @created    30-apr-2011
 */
class Classes_Users extends Classes_DbTable_Table
{

    /**
     * Mapping al table model
     */
    protected $_table	= 'users';
    protected $_primary = 'user_id';

    public function fetchById( $id )
    {
	if( !is_numeric( $id ) )
	    throw new Classes_Exceptions( 'UserId Invalido: '. $id );
	
	$sql_data = array( 0 => $this->_table, // tabella
            1 => array( '*' ), //
            2 => array( 'user_id' => $id ), // where
            3 => array( DBTABLE_PREFIX . 'user_groups' => array( DBTABLE_PREFIX . 'user_groups.group_id', DBTABLE_PREFIX . 'users.user_id' ) ) // Left Join
        );

        $result = $this->db->simpleSelectQuery( $sql_data );

	if( $result->num_rows > 0 )
	{
	    return $result->fetch_assoc();	
	}
    }

    public function fetchAll( array $params = array() )
    {
	$sql_data = array( 
	    'fields' => array( '*', 'CONCAT(user_firstname, \' \', user_lastname) AS user_name' ),
            'join' => array( DBTABLE_PREFIX . 'user_groups' => array( DBTABLE_PREFIX . 'user_groups.group_id', DBTABLE_PREFIX . 'users.user_group_id' ) ), // Left Join
	    'orderby' => ( count( $params['orderby'] ) > 0 ) ? $params['orderby'] : NULL,
	    'limit' => ( count( $params['limit'] ) == 2 ) ? $params['limit'] : NULL
        );

	return parent::fetchAll( $sql_data );
    }

    public function fetchByEmail( $email )
    {
	$sql_data = array( 'from' => $this->_table, // tabella
            'fields' => array( '*', 'DATE_FORMAT(user_lastlogin,\'%d-%m-%Y\') AS post_published' ), //
            'where' =>  array( DBTABLE_PREFIX . 'users.user_email' => $email ), // where
        );

        $result = $this->db->selectQuery( $sql_data );

	if( $result->num_rows > 0 )
	{
	    while( $data = $result->fetch_assoc() )
	    {
		$ret[$data['user_id']] = $data;
	    }
	}

        return $ret;
    }
    
    public function save()
    {
	if( is_null( $this->user_email ) )
	    throw new Classes_Exceptions( 'Errore $_user_email: '. $this->user_email );

	if( is_null( $this->user_id ) OR ( $this->user_id == '' ) )
	{
	    $this->user_created = 'now';
	    
	    if( is_null( $this->user_createdby ) OR ( $this->user_createdby == '' ) )
		throw new Classes_Exceptions( 'user_createdby Invalido '. $this->user_createdby );	    
	}
	else
	{
	    $this->user_modified = 'now';
	    
	    if( is_null( $this->user_modifiedby ) OR ( $this->user_modifiedby == '' ) )
		throw new Classes_Exceptions( 'user_modifiedby Invalido '. $this->user_modifiedby );	    	    
	}

	# Filtriamo...
	$filters = array(
	    'user_id' => FILTER_SANITIZE_NUMBER_INT,
	    'user_name' => FILTER_SANITIZE_STRING,
	    'user_surname' => FILTER_SANITIZE_STRING,
	    'user_password' => NULL,
	    'user_email' => FILTER_SANITIZE_EMAIL,
	    'user_group_id' => FILTER_SANITIZE_NUMBER_INT,
	    'user_created' => NULL,
	    'user_createdby' => FILTER_SANITIZE_NUMBER_INT,
	    'user_modified' => NULL,
	    'user_modifiedby' => FILTER_SANITIZE_NUMBER_INT,
	);

	$this->setOptions( ( filter_var_array( $this->toArray(), $filters ) ) );	

	if( !is_null( $this->user_password ) )
	    $this->user_password = md5( $this->user_password );
	
	return parent::save();
    }
}
